import {observable, computed, action} from 'mobx'
import {createContext} from 'react'
import ChildrenPageUtils from '@/store/extends'
import {uCancelShareDev, uSelectMyShareData} from '@/api/setting'
import {getStorage} from '@/utils/localstoreage'
import Taro from '@tarojs/taro'

class Dissharedev extends ChildrenPageUtils {
  @observable pageQuery: {
    devId: string
    devName: string
    shareUserName: string
    shareTime: string
    userDevId: string
  } = {
    devId: '',
    devName: '',
    shareUserName: '',
    shareTime: '',
    userDevId: ''
  }
  @observable detailData: Setting.MyShareData = {
    shareCount: 0,
    shareLinkDevCount: 0,
    passiveShareCount: 0
  }

  @action.bound
  async updateDetailData() {
    let userId = getStorage('userId')
    if (!userId) return
    Taro.showLoading({
      title: '加载中...'
    })
    let res = await uSelectMyShareData({devId: this.pageQuery.devId, userId})
    if (res && res.result === 'success') {
      this.detailData = res.data
    } else {
      this.detailData = {
        shareCount: 0,
        shareLinkDevCount: 0,
        passiveShareCount: 0
      }
    }

    Taro.hideLoading()
  }

  @action.bound
  async disShareDev() {
    if (!this.pageQuery.userDevId) return

    let res = await uCancelShareDev({
      userDevId: this.pageQuery.userDevId
    }).catch(err => {})
    if (res) {
      if (res.result === 'error' && res.msg) {
        Taro.showModal({
          content: res.msg
        })
      }
      if (res.result === 'success') {
        Taro.navigateBack()
      }
    }
  }
}
export const disShareDevStore = createContext(new Dissharedev())
